using System;
using System.Collections.Specialized;
using System.Xml;
using System.Collections.Generic;

namespace FreeFlow.Client
{
	/// <summary>
	/// A list field.
	/// </summary>
	[Serializable]
	public class ListField : Field
	{
    internal ListField(Form form, XmlElement fieldNode)
      : base(form, fieldNode)
		{
		}

    #region options
    private List<string> options = new List<string>();
    /// <summary>
    /// Gets the number of options in the list.
    /// </summary>
    public int OptionCount
    {
      get
      {
        return options.Count;
      }
    }

    /// <summary>
    /// Gets the option at the specified index.
    /// </summary>
    /// <param name="index">Index.</param>
    /// <returns></returns>
    public string Option(int index)
    {
      return options[index];
    }
    #endregion
    #region selected items

    private List<string> selectedItems = new List<string>();

    /// <summary>
    /// Gets the number of selected items.
    /// </summary>
    public int SelectedCount
    {
      get 
      {
        return selectedItems.Count;
      }
    }

    /// <summary>
    /// Gets the slected item at the specified index.
    /// </summary>
    /// <param name="index">Index.</param>
    public string Selected(int index)
    {
      return selectedItems[index];
    }

    #endregion

    internal override void FillValue(XmlElement node)
    {
      base.FillValue(node);
      FillFromOptionList(options, node);
    }

    /// <summary>
    /// Gets or sets the value of the field.
    /// </summary>
    public override string Value
    {
      get { return base.Value; }
      set 
      { 
        base.Value = value; 
        // selected items
        selectedItems.Clear();
        string[] selected = Value.Split(new char[] {','});
        foreach(string item in selected)
        {
          if ((item != null) && (item.Length > 0))
            selectedItems.Add(item);
        }
      }
    }
	}
}
